package com.xuanxuan.usbdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

import com.ywicc.usblibs.UsbService;
import com.ywicc.usblibs.utils.UsbUtils;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, UsbService.OnUsbRecvDataListener {

    private final static String TAG = "MainActivity";
    private UsbService usbService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initService();

    }

    public void initView() {
        findViewById(R.id.btn_start).setOnClickListener(this);
        findViewById(R.id.btn_stop).setOnClickListener(this);
        findViewById(R.id.btn_car_run).setOnClickListener(this);
        findViewById(R.id.btn_car_stop).setOnClickListener(this);
    }

    public void initService() {
        Intent intent = new Intent(MainActivity.this, UsbService.class);
        startService(intent);

        // 延时绑定服务
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                bindService(intent, connection, Context.BIND_AUTO_CREATE);
            }
        });
    }

    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            UsbService.UsbBinder binder = (UsbService.UsbBinder) iBinder;
            usbService = binder.getService();

            usbService.registerListener(MainActivity.this, 1000);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            // 空实现
        }
    };

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(MainActivity.this, UsbService.class);
        switch (view.getId()) {
            case R.id.btn_start:
                initService();
                break;
            case R.id.btn_stop:
                usbService.destroyListener();
                break;
            case R.id.btn_car_run:
                startAuto();
                break;
            case R.id.btn_car_stop:
                byte[] stop = new byte[]{0x55, (byte) 0xAA, 0x01, 0x00, 0x00, 0x00, 0x01, (byte) 0xBB};
                usbService.sendData(stop);
                break;
        }
    }

    public void startAuto() {
        short MAJOR = 0xAA;
        short FIRST = (byte) 0x00;
        short SECOND = (byte) 0x00;
        short THRID = (byte) 0x00;
        short CHECKSUM=(short) ((MAJOR+FIRST+SECOND+THRID)%256);
        byte[] go = new byte[]{0x55, (byte) 0xAA, (byte) MAJOR, (byte) FIRST, (byte) SECOND, (byte) THRID ,(byte) CHECKSUM,(byte) 0xBB};
        usbService.sendData(go);
        Log.w(TAG, "发送成功");
    }


    @Override
    public void callBackRecvData(byte[] data) {
        Log.w(TAG, "输出数据为: " + Arrays.toString(data));
    }
}